/* SVN FILE: $Id: builds_controller.js 50 2008-11-03 22:26:02Z killergeorge@gmail.com $ */
/**
 * Short description for file.
 *
 * jQuery is required
 * jamal is required
 * JamalBuilder:  Javascript MVC Assembler
 * Copyright (c)  2007, Georg Leciejewski <http://blog.mediaprojekte.de/>
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright        Copyright (c) 2007, Georg Leciejewski
 * @link            
 * @package          JamalBuilder
 * @subpackage       jamal.controllers.builds
 * @since            JamalBuilder v 0.1
 * @version          $Revision: 50 $
 * @modifiedby       $LastChangedBy: killergeorge@gmail.com $
 * @lastmodified     $Date: 2008-11-03 22:26:02 +0000 (Mon, 03 Nov 2008) $
 * @license          http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * BuildsController
 * 
 * 
 *
 */
$j.c({Builds: {
   	
	/**
	 * directory - keeps the name of the current top working directory
	 * might be put somewhere else in the future ... model dude
	 * controller wide var
	 * @param object - holds an air file object
	 */
	directory: new air.File(),
	/**
	 * tmp_file - keeps the name of the current top working directory
 	 * the content of the js files will be appended to this file 
 	 * before the compression takes place
	 * @param object - holds an air file object in the temp dir of the system
	 */
	tmp_file:null,
	
	/**
	 * a sound object.
	 * global so only one sound is played at a time .. not working yet must check
	 */
	sound_channel: null,
    	
    /**
     * Build index
     * where you select a folder and build jamal
     */
    index: function () {
    	jamal.log('Start BuildsController index');
				
		//add listener do global directory object
		$j.current.directory.addEventListener( air.Event.SELECT, $j.current.doSelect );
			
		//add browse button event
		$( "#btnBrowse" )[0].addEventListener( "click", $j.current.doBrowse );	
		//add save button even
		$( "#btnPackJamal" )[0].addEventListener( "click", $j.current.startBuildJamal );
		$( "#btnPackApp" )[0].addEventListener( "click", $j.current.startAppBuild);
		$( "#btnPackAll" )[0].addEventListener( "click", $j.current.startAllBuild);

		//show buttons
		$('#btnPackJamal').hide();
		$('#btnPackApp').hide();
		$('#btnPackAll').hide();

		//add drag enter event
		window.htmlControl.addEventListener(air.dragenter, $j.current.doEnter ); 
		//remove it when windows is changed
		$j.current.addToGarbage(air.dragenter, $j.current.doEnter ); 
		
		//dragging out of range
		window.htmlControl.addEventListener( air.dragleave, $j.current.doDragExit );
		//remove it when windows is changed
		$j.current.addToGarbage(air.dragleave, $j.current.doDragExit ); 
		
		//add drag drop event
		window.htmlControl.addEventListener(air.drop, $j.current.doDrop );
		//remove it when windows is changed
		$j.current.addToGarbage(air.drop, $j.current.doDrop ); 
				
		//read content of navigation and insert into navi div
		this.appendMainNav('Build');
			
		if(!jamal.airCache.get('first_time'))
		{	
			jamal.airSettings.applyWindow();
			jamal.airCache.set('first_time','true');
			//add global closing handler here cause this is the fist function called
			window.nativeWindow.addEventListener(air.Event.CLOSING, $j.current.windowClosingHandler); 
		}				
    },


/****************************************************
 * Actions for events
****************************************************/

	

	/**
	 * when a file is dragged over the air window
	 * @param Object e - tranferable file object .. need to lookop air docs in detail .. me lazy bud
	 */
	doEnter: function (e) {
		jamal.log('Start BuildsController doEnter');
		
		if( e.transferable.hasFormat( air.TransferableFormats.FILE_LIST_FORMAT ) )
		{			
			air.DragManager.acceptDragDrop( window.htmlControl );
			//e.dataTransfer.dropEffect = "copy"; //must check the specs what else is available
			jamal.message.show('Watch it babe ... Jamal Directory incoming','file_incoming');
		}		
		if(jamal.settings.get('use_sound'))
		{
			urlReq = new air.URLRequest("app-resource:/soundz/jet_plane.mp3");
			$j.current.sound_channel = new air.Sound(urlReq);
			$j.current.sound_channel.play();	
		}	
	},
	
	/**
	 * when a formerly dragged over file is dragged away for the air window
	 * @param Object e - tranferable file object .. need to lookop air docs in detail .. me lazy bud	
	 */
	doDragExit: function (e) {
		jamal.log('Start BuildsController doDragExit');		
		jamal.message.close();
	},


	/**
	 * when a folder is dropped in the air window
	 */
	doDrop: function(e)	{	
	
		jamal.log('Start BuildsController doDrop');

		var files = e.transferable.dataForFormat( air.TransferableFormats.FILE_LIST_FORMAT, air.TransferableTransferMode.ORIGINAL_PREFERRED );
	
		if(files.length == 1 && files[0].isDirectory)
		{//only allow one folder
			//clear files/xml/ul list
			$('#fileList').empty();
			html_list = '';
			xml_list = '';
			
			//set global home directory
			$j.current.directory = files[0];
			//remove drop box
			$('#fileIncoming').slideUp();
			$('#fileIncoming').empty();	
				
			$j.current._generateHtmlTree($j.current.directory);
			
			$('#fileList').append(html_list);
			
			$('#fileList').Treeview({
				speed: "fast",
				collapsed: true,
				unique: true,
				store: false			
			});				
			//show buttons
			$('#btnPackJamal').show();
			$('#btnPackApp').show();			
			
			if(jamal.settings.get('use_sound'))
			{				//sound
				var urlReq = new air.URLRequest("app-resource:/soundz/water_drop.mp3");
				$j.current.sound_channel = new air.Sound(urlReq);
				$j.current.sound_channel.play();				
			}			
			jamal.message.close();

		}
		else
		{
			jamal.message.show('Please drag only one Directory onto this Window.','error_msg');
		}	
	},

	/**
	 * open the native file dialog
	 */
	doBrowse: function () {
		jamal.log('Start BuildsController doBrowse');
		$j.current.directory.browseForDirectory( "Select jamal directory:" );		
		
	},
	
	/**
	 * Listener function for Event: air.Event.SELECT
	 * Occurs after a directory is selected in the native filedialog popup.	 * 
	 * The selected dir is gone through recursivly and a ul/li list of its content is rendered
	 */
	doSelect: function () {
		jamal.log('Start BuildsController doSelect');
		//clear existing list values
		$('#fileList').empty();
		
		//this var ist not declared global but still it is filled within the next called function _generateHtmlTree ???
		html_list = '';
	
		$j.current._generateHtmlTree($j.current.directory);
		
		$('#fileList').append(html_list);
		
		$('#fileList').Treeview({
			speed: "fast",
			collapsed: true,
			unique: true,
			store: false			
		});		
		//show buttons
		$('#btnPackJamal').show();
		$('#btnPackApp').show();
	},	
	
	
/****************************************************
 * Start Build processing
****************************************************/

	/**
	 * initalize a new build
	 * - creates a new tempfile which will be used to concat the js files first
	 */
	startBuildJamal: function(){
		jamal.log('Start BuildsController startBuildJamal');
	
		//create new temp file
		$j.current.tmp_file = air.File.createTempFile( );
		jamal.log('Tempfile: ' + $j.current.tmp_file.nativePath);
		//start the process
		$j.current.readIntro();	
					
	},
	
	/**
	 * read the intro file
	 * @param {Object} base_dir
	 */
	readIntro: function () {
		jamal.log('Start BuildsController readIntro');
		//READ
		var data 	= $j.current._readFile("/jamal/intro.js");

		//WRITE
		tmp_stream = new air.FileStream();
		tmp_stream.open( $j.current.tmp_file, air.FileMode.APPEND );	
		//append data to temp file
		tmp_stream.writeMultiByte(data, air.File.systemCharset );
		tmp_stream.close();	
		
		//on to next
		$j.current.readJamal();
		
	},

	/**
	 * read Jamal core file
	 */
	readJamal: function () {
		jamal.log('Start BuildsController readJamal');
		
		var data 	= $j.current._readFile("/jamal/jamal.js");
		var ok = $j.current._appendToTempFile(data);
		if(ok == true)
		{//on to next
			$j.current.readLibs();	
		}
		else
		{//some error handling here
		
			
		}
	},
	
	/**
	 * read content of /libs folder
	 */
	readLibs: function () {
		jamal.log('Start BuildsController readLibs');
		
		var base_dir 	= $j.current.directory.nativePath;		
		var data 		= null;
		var ok	 		= null;
		
		var dir 		= new air.File();
		dir			 		= dir.resolvePath(base_dir + "/jamal/libs/" );	
		var files 	= dir.getDirectoryListing();		
		
		for (var i = 0; i < files.length; i++) 
		{// each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')   
			{//parking only js
		    	jamal.log("read Lib:" + files[i].name);
				//READ			
				data = $j.current._readFile(files[i]);			
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here				
			}					
		}
		//pass it on to the component reader
		$j.current.readComponents();	
		
	},
	/**
	 * read the content of the components folder and appen each file to the temp file
	 */
	readComponents: function ()	{
		jamal.log('Start BuildsController  readComponents');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/jamal/components/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (var i = 0; i < files.length; i++) 
		{//for each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}

		$j.current.readStartup();
		
	},
	/**
	 * read the content of the components folder and appen each file to the temp file
	 */
	readStartup: function () {
		jamal.log('Start BuildsController readStartup');	
		//READ	
		var data = $j.current._readFile("/jamal/startup.js");	
		//WRITE
		var ok = $j.current._appendToTempFile(data);
		if(ok == true)
		{
			$j.current.readPlugins();	
		}
		else
		{//some error handling here
			
		}				
	},

	/**
	 * read the contents of the plugins directory
	 * watch out there is no check for subdirectories right now
	 */
	readPlugins: function () {
		jamal.log('Start BuildsController  readPlugins');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/jamal/plugins/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (i = 0; i < files.length; i++) 
		{//for each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}
		//onto next
		$j.current.readOutro();		
	},
	
	/**
	 * read the closer file, which just contains a closing bracket
	 */
	readOutro: function () {
		jamal.log('Start BuildsController readOutro');
		
		//READ	
		var data = $j.current._readFile("/jamal/outro.js");		
		//WRITE
		var ok = $j.current._appendToTempFile(data);
				
		if(ok == true)
		{//onto next
			$j.current.moveTempFile();	
		}
		else
		{//some error handling here		
			
		}	
	},

	/**
	 * move the temp file containing the new concatenated file 
	 * into the current working directory
	 */
	moveTempFile: function () {
		jamal.log('Start BuildsController  moveTempFile');
		
		var base_dir	= $j.current.directory.nativePath;
		var jamal_debug = $j.current.directory.resolvePath(base_dir + "/jamal_debug.js");
		var msg			= '';
		
		try  
		{
		    $j.current.tmp_file.moveTo(jamal_debug, true);
			
		}
		catch (error)
		{
			jamal.log("Error:" + error.message);
		}
		finally
		{
			msg = '<b>Jamal debug:</b> <br />' + jamal_debug.nativePath;
			jamal.message.show(msg, 'success_msg');			
			$j.current.compressJamalCore();		
		}
	},

	/**
	 * compress the jamal core file 
	 * which is the newly generated concat debug version of Jamal
	 */
	compressJamalCore: function () {
		jamal.log('Start BuildsController  compressJamalCore');
		
		var base_dir	= $j.current.directory.nativePath;
		var stream		= null;	
		var packed_file	= new air.File();
		var packed_data	= null;
		
		//READ	
		var data = $j.current._readFile("/jamal_debug.js");
			
		//the packing .. pretty short for what it does :-)
		packed_data = pack( data, 62, true, false );	
		
		
		//init packed_file;	
		packed_file		= packed_file.resolvePath(base_dir + '/jamal_packed.js' );
		
		//WRITE
		stream = new air.FileStream();
		stream.open( packed_file, air.FileMode.WRITE );	
		//append data to temp file
		stream.writeMultiByte(packed_data, air.File.systemCharset );
		//close stream
		stream.close();	
		
		//add xml save
		var config_save_html = '<br />'+
							'<input id="configName" type="text" value="Projectname" /> '+
							'<input id="btnSaveConfig" type="button" value="Save Project" />';
		
		$('#btnStartBuildProcess').after(config_save_html);
		
		$( "#btnSaveConfig" )[0].addEventListener( "click", $j.current.doSaveXml );	
		
		msg = '<br /><b>Jamal compressed:</b> <br />' + packed_file.nativePath;
		jamal.message.show(msg, 'success_msg',true);
		
	},
	
	/***************************************
	 * Build App
	 *
	 * - make new temp file
	 * - read models
	 * - read controller
	 * - read views
	 * - write uncompressed
	 * - write compressed
	***************************************/
	
	/**
	 * Start the app/ build
	 * - creates a new tempfile which will be used to concat the js files first
	 */
	startAppBuild: function(){
		jamal.log('Start BuildsController startBuildProcess');
		//create new temp file
		$j.current.tmp_file = air.File.createTempFile( );
		jamal.log('Tempfile: ' + $j.current.tmp_file.nativePath);
		//start the process
		$j.current.readAppMvc();	
	},
	
	/**
	 * read App core files 
	 * - app_controller
	 * - app_model
	 * - app_view
	 */
	readAppMvc: function () {
		jamal.log('Start BuildsController readAppMvc');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/app/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (i = 0; i < files.length; i++) 
		{//for each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}
		//onto next
		$j.current.readModels();		
	
	},
	/**
	 * read the contents of the App/Models directory
	 */
	readModels: function ()	{
		jamal.log('Start BuildsController  readModels');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/app/models/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (i = 0; i < files.length; i++) 
		{//for each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}
		//onto next
		$j.current.readControllers();		
	},
	
	/**
	 * read the contents of the App/Controllers directory
	 */
	readControllers: function () {
		jamal.log('Start BuildsController  readControllers');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/app/controllers/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (i = 0; i < files.length; i++) 
		{//for each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}
		//onto next
		$j.current.readViews();		
	},
	
	/**
	 * read the contents of the App/Controllers directory
	 */
	readViews: function () {
		jamal.log('Start BuildsController  readViews');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/app/views/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (i = 0; i < files.length; i++) 
		{//for each file in libs
			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}
		//onto next
		$j.current.readAppPlugins();		
	},
	
	/**
	 * read the contents of the plugins directory
	 * watch out there is no check for subdirectories right now
	 */
	readAppPlugins: function () {
		jamal.log('Start BuildsController  readAppPlugins');
		
		var base_dir 	= $j.current.directory.nativePath;	
		var data 		= null;
		var ok 			= null;
	
		var dir 		= new air.File();
		dir			 	= dir.resolvePath(base_dir + "/app/plugins/" );	
		var files 		= dir.getDirectoryListing();	
	
		for (i = 0; i < files.length; i++) 
		{//for each file in libs
//			if(files[i].name.substring(files[i].name.length-3) == '.js')  
			if(files[i].name.lastIndexOf('.js') !== -1)  
			{//only do this for shiny js files			
				//READ	
				data = $j.current._readFile(files[i]);
				//WRITE
				ok = $j.current._appendToTempFile(data);	
			 	//need some error check here		
			}				
		}
		//onto next
		$j.current.moveAppTempFile();		
	},
	
	/**
	 * move the temp file containing the new concatenated app/.. files 
	 * into the current working directory
	 */
	moveAppTempFile: function () {
		jamal.log('Start BuildsController  moveTempFile');
		
		var base_dir	= $j.current.directory.nativePath;
		var app_debug = $j.current.directory.resolvePath(base_dir + "/app_debug.js");
		var msg			= '';
		
		try  
		{
		    $j.current.tmp_file.moveTo(app_debug, true);
			
		}
		catch (error)
		{
			jamal.log("Error:" + error.message);
		}
		finally
		{
			msg = '<b>App debug:</b> <br />' + app_debug.nativePath;
			jamal.message.show(msg, 'success_msg');			
			$j.current.compressApp();		
		}
	},
	
	/**
	 * compress the app file 
	 * which is the newly generated concat debug version of Jamal
	 */
	compressApp: function () {
		jamal.log('Start BuildsController  compressApp');
		
		var base_dir	= $j.current.directory.nativePath;
		var stream		= null;	
		var packed_file	= new air.File();
		var packed_data	= null;
		
		//READ	
		var data = $j.current._readFile("/app_debug.js");
			
		//the packing .. pretty short for what it does :-)
		packed_data = pack( data, 62, true, false );			
		
		//init packed_file;	
		packed_file		= packed_file.resolvePath(base_dir + '/app_packed.js' );
		
		//WRITE
		stream = new air.FileStream();
		stream.open( packed_file, air.FileMode.WRITE );	
		//append data to temp file
		stream.writeMultiByte(packed_data, air.File.systemCharset );
		//close stream
		stream.close();	
		
		urlReq = new air.URLRequest("app-resource:/soundz/paper_01.mp3");
		$j.current.sound_channel = new air.Sound(urlReq);
		$j.current.sound_channel.play();
		
		msg = '<br /><b>App compressed:</b> <br />' + packed_file.nativePath;
		jamal.message.show(msg, 'success_msg',true);
		//show pack All Button
		$('#btnPackAll').show();
				
	},
	
	/*************************************************
	 * Build All, takes the jamal core debug file 
	 * and the app debug file and melts them together
	 * 
	 ************************************************/
	
	startAllBuild: function () {
		jamal.log('Start BuildsController startAllBuild');
		//create new temp file
		$j.current.tmp_file = air.File.createTempFile( );
		jamal.log('Tempfile: ' + $j.current.tmp_file.nativePath);
		//start the process
		$j.current.readJamalDebug();	
	},
	
	/**
	 * append the jamal debug version to temp file
	 */
	readJamalDebug: function () {
		jamal.log('Start BuildsController  readJamalDebug');
		//READ	
		var data = $j.current._readFile("/jamal_debug.js");		
		//WRITE
		var ok = $j.current._appendToTempFile(data);
				
		if(ok == true)
		{//onto next
			$j.current.readAppDebug();	
		}
		else
		{//some error handling here		
			
		}	
		
	},
	/**
	 * append the App debug version to temp file
	 */
	readAppDebug: function () {
		jamal.log('Start BuildsController  readAppDebug');
		//READ	
		var data = $j.current._readFile("/app_debug.js");		
		//WRITE
		var ok = $j.current._appendToTempFile(data);
				
		if(ok == true)
		{//onto next
			$j.current.moveAllTempFile();	
		}
		else
		{//some error handling here		
			
		}			
	},
	
	/**
	 * move the temp file containing the new concatenated all file 
	 * into the current working directory
	 */
	moveAllTempFile: function () {
		jamal.log('Start BuildsController  moveTempFile');
		
		var base_dir	= $j.current.directory.nativePath;
		var all_debug = $j.current.directory.resolvePath(base_dir + "/all_debug.js");
		var msg			= '';
		
		try  
		{
		    $j.current.tmp_file.moveTo(all_debug, true);
			
		}
		catch (error)
		{
			jamal.log("Error:" + error.message);
		}
		finally
		{
			msg = '<b>All debug:</b> <br />' + all_debug.nativePath;
			jamal.message.show(msg, 'success_msg');			
			$j.current.compressAll();		
		}
	},
	/**
	 * makes compressed version of all files
	 */
	compressAll: function () {
		jamal.log('Start BuildsController  compressAll');
		
		var base_dir	= $j.current.directory.nativePath;
		var stream		= null;	
		var packed_file	= new air.File();
		var packed_data	= null;
		
		//READ	
		var data = $j.current._readFile("/all_debug.js");
			
		//the packing .. pretty short for what it does :-)
		packed_data = pack( data, 62, true, false );			
		
		//init packed_file;	
		packed_file		= packed_file.resolvePath(base_dir + '/all_packed.js' );
		
		//WRITE
		stream = new air.FileStream();
		stream.open( packed_file, air.FileMode.WRITE );	
		//append data to temp file
		stream.writeMultiByte(packed_data, air.File.systemCharset );
		//close stream
		stream.close();
		
		msg = '<br /><b>All compressed:</b> <br />' + packed_file.nativePath;
		jamal.message.show(msg, 'success_msg',true);	

	},

	/***************************************
	 * Protected Helpers
	***************************************/
	
	/**
	 * takes a single top directory and recursivly builds up an ul/li list
	 * the list is saved in a global variable
	 * <ul>
	 * <li><img src="img/folder.gif" />Item 1
	 * 	<ul>
	 *	   <li><img src="img/file.gif" />Item 1.1</li>
	 *	</ul>
	 *	</li>
	 * </ul>
	 * function taken from http://www.devx.com/xml/Article/11866/1954?pf=true
	 * @param {Object} dir
	 */
	_generateHtmlTree: function (file_oject) {
		
		if(file_oject.isDirectory)
		{//got a freakin directory
			html_list +='<li><img src="img/folder.gif" /> ' + file_oject.name + '<ul>';
				
			var sub_nodes= file_oject.getDirectoryListing();		
			for(var i=0; i<sub_nodes.length; i++)
			{
				$j.current._generateHtmlTree(sub_nodes[i]);			
			}
	        html_list +='</li></ul>';		
		}
		else
		{//its a file
			html_list +='<li><img src="img/file.gif" /> ' + file_oject.name + '</li>';
		}	
	},
	
	/**
	 * read a single file and return its contents
	 * @param {MIXED} file - STRING - the filename or a path/filename.js
	 * 									in this case a new file object will be created
	 * 						OBJECT - an existing file object
	 */
	_readFile: function (file) {
		jamal.log("Start _readFile");
		//jamal.log(typeof file);
		if(typeof file == 'string')
		{//passed a filename string in so need to make a new fileobject
			var filename 	= file;	//cosmetics
			var base_dir	= $j.current.directory.nativePath;
			var file 	 	= new air.File();
			file 			= file.resolvePath(base_dir + filename );
		}	
		
		var stream = new air.FileStream();
		stream.open( file, air.FileMode.READ );
		data = stream.readMultiByte(file.size, air.File.systemCharset);
		stream.close();	

		return data;
	},

	
	/**
	 * append content to the temp file
	 * @param {String} data - the date to append to
	 */
	_appendToTempFile:function (data) {
		console.log("Start _appendToTempFile");
		//maybe hav a global stream open insted opening one each time???
		var tmp_stream = new air.FileStream();
		tmp_stream.open( $j.current.tmp_file, air.FileMode.APPEND );	
		//append data to temp file
		tmp_stream.writeMultiByte(data, air.File.systemCharset );
		tmp_stream.close();	
		
		return true;	
	}
	
}
});
